/*
 * Copyright 2010. 
 * 
 * This document may not be reproduced, distributed or used 
 * in any manner whatsoever without the expressed written 
 * permission of xrkol Corp. 
 * 
 * $Rev: Rev $
 * $Author: Author $
 * $LastChangedDate: LastChangedDate $
 *
 */

package com.xrkol.sharelib.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xrkol.sharelib.dao.ClassDao;
import com.xrkol.sharelib.dao.TeacherDao;
import com.xrkol.sharelib.dao.TeachingDao;
import com.xrkol.sharelib.entity.Clazz;
import com.xrkol.sharelib.entity.Student;
import com.xrkol.sharelib.entity.Teacher;
import com.xrkol.sharelib.entity.Teaching;
import com.xrkol.sharelib.service.ClassService;
import com.xrkol.sharelib.service.TeacherService;
import com.xrkol.sharelib.util.CollectionUtils;
import com.google.common.collect.Lists;

/**
 * @author peng.xia
 * 
 */
@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao userDao;

    @Autowired
    private TeachingDao teachingDao;

    @Autowired
    private ClassService classService;
    @Autowired
    private ClassDao classDao;

    @Override
    public void save(Teacher entity) {
        this.userDao.save(entity);
    }

    @Override
    public void update(Teacher entity) {
        this.userDao.update(entity);
    }

    @Override
    public void delete(String id) {
        this.userDao.delete(id);
    }

    @Override
    public Teacher findById(String id) {
        return this.userDao.findById(id);
    }

    @Override
    public Teacher findByUserName(String string) {
        return this.userDao.findByUserName(string);
    }

    @Override
    public List<Teacher> findTeachersWithPagination(String schoolId, Integer page) {
        return this.userDao.findTeachersWithPagination(schoolId, page);
    }

    @Override
    public List<Teacher> getAllTeachers(String schoolId) {
        return this.userDao.getAllTeachers(schoolId);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Teacher> findTeachersByStudents(List<Student> students) {
        List<Teacher> teachers = Lists.newArrayList();
        List<Clazz> clazzs = this.classService.findAllClassesByStudents(students);
        for (Clazz clazz : clazzs) {
            Teacher adviser = clazz.getAdviser();
            if (adviser != null) {
                adviser.setAdviser(true);
                teachers.add(adviser);
            }
            List<Teaching> teachings = this.teachingDao.getByClass(clazz);
            for (Teaching teaching : teachings) {
                Teacher teacher = teaching.getTeacher();
                if (!(adviser != null && adviser.getId().equals(teacher.getId()))) {
                    teachers.add(teacher);
                }
            }
        }
        return CollectionUtils.removeDuplicate(teachers);
    }

    @Override
    public Teacher findByUserId(String userId) {
        return this.userDao.findByUserId(userId);
    }

    @Override
    public boolean checkExist(Teacher teacher) {
        return this.userDao.checkExist(teacher);
    }

    @Override
    public List<Teacher> getTeachersByClass(Clazz clazz) {
        List<Teaching> teachings = this.teachingDao.getByClass(clazz);
        List<Teacher> teachers = Lists.newArrayList();
        for (Teaching teaching : teachings) {
            teachers.add(teaching.getTeacher());
        }
        return teachers;
    }

    @Override
    public boolean isAdvisor(Teacher teacher) {
        List<Clazz> classes = this.classDao.getAllClasses(teacher.getSchoolId());
        for (Clazz clz : classes) {
            if (teacher.equals(clz.getAdviser())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isTheching(Teacher teacher) {
        List<Teaching> teachings = this.teachingDao.getByTeacher(teacher);
        return teachings.isEmpty() ? false : true;
    }

    @Override
    public List<Teacher> findManagers(String schoolId, Integer page) {
        return userDao.findManagersWithPaging(schoolId, page);
    }
}
